package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.ShoppingCart;
import com.itheima.config.BaseContext;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.exception.CustomException;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartDao shoppingCartDao;

    @Override
    public List<ShoppingCart> findByUid(Long id) {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,id);
        return shoppingCartDao.selectList(lqw);
    }

    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getThreadLocal());
        lqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartDao.selectOne(lqw);
        if (one!=null){
            one.setNumber(one.getNumber()+1);
            int i = shoppingCartDao.updateById(one);
            if (i > 0) {
                return one;
            }
            return null;
        }
        shoppingCart.setUserId(BaseContext.getThreadLocal());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);
        if (shoppingCartDao.insert(shoppingCart)>0){
            return shoppingCart;
        }
        return null;
    }

    @Override
    public int clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getThreadLocal());
        return shoppingCartDao.delete(lqw);
    }

    @Override
    public ShoppingCart sub(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getThreadLocal());
        lqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart shoppingCart1 = shoppingCartDao.selectOne(lqw);
        if (shoppingCart1.getNumber()==1){
            if (shoppingCartDao.delete(lqw)>0){
                return null;
            }
            throw new CustomException("修改失败！");
        }
        shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
        if (shoppingCartDao.updateById(shoppingCart1)>0){
            return shoppingCart1;
        }
        return shoppingCartDao.selectOne(lqw);
    }
}
